package com.zy.pro.shop.service.impl;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.zy.core.service.impl.ServiceSupport;
import com.zy.pro.security.dao.SecurityMapper;
import com.zy.pro.security.model.Security;
import com.zy.pro.security.web.form.SecuritySearchForm;
import com.zy.pro.shop.dao.ShopMapper;
import com.zy.pro.shop.dao.ShopUserMapper;
import com.zy.pro.shop.model.Shop;
import com.zy.pro.shop.model.ShopUser;
import com.zy.pro.shop.service.ShopService;
import com.zy.utils.BigDecimalUtil;
import com.zy.utils.MethodUtil;
@Service
public class ShopServiceImpl extends ServiceSupport<Shop> implements ShopService {

	@Autowired
    private ShopMapper<Shop> mapper;
	
	@Autowired
    private ShopUserMapper<ShopUser> shopUserMapper;
	
	@Autowired
    private SecurityMapper<Security> securityMapper;

	public ShopMapper<Shop> getMapper() {
		return mapper;
	}

	@Override
	public void saveShopAndShopUser(Shop shop) throws Exception {
		Date now = new Date();
		//店铺状态
		shop.setShopStatus(1);//正常使用
		shop.setShopCreateTime(now);
		//判断是否已经缴付保障金,是时设置 店铺信誉度
		if(shop.getShopBondStatus() == 2 && shop.getShopAllMoney() != null){
			List<Security> sList = securityMapper.findAll(new SecuritySearchForm());
			if(!sList.isEmpty()){
				//保障金信誉度
				shop.setShopMoneyCredibility(new BigDecimal(sList.get(0).getSecurityCredibility()));
				//总信誉度
				shop.setShopAllCredibility(BigDecimalUtil.add(shop.getShopMoneyCredibility(), shop.getShopCredibility()));
			}
		}
		
		//插入店铺信息
		mapper.insert(shop);
		//插入店家信息
		ShopUser shopUser = shop.getShopUser();
		//shopUser.setShopUserName(shop.getShopUser().getShopUserName());
		//店铺主键
		shopUser.setShopId(shop.getShopId());
		//初始化密码123456
		shopUser.setPassword(MethodUtil.MD5("123456"));
		shopUser.setShopUserCreateTime(now);
		shopUserMapper.insert(shopUser);
	}

	@Override
	public void updateShop(Shop shop) throws Exception {
		Shop info = mapper.selectById(shop.getShopId());
		if(info != null){
			//城市主键
			info.setCityId(shop.getCityId());
			//店铺名称
			info.setShopName(shop.getShopName());
			//联系电话
			info.setContactTel(shop.getContactTel());
			//联系人
			info.setShopContact(shop.getShopContact());
			//联系地址
			info.setShopAddress(shop.getShopAddress());
			//简介
			info.setShopIntro(shop.getShopIntro());
			//店铺图片
			info.setShopLogo(shop.getShopLogo());
			//判断如果店铺的保障金支付状态是否发生变动,如果发送变动则根据变动的情况设置,店铺的信誉度和总信誉度
			if(info.getShopBondStatus() != shop.getShopBondStatus()){
				if(shop.getShopBondStatus() == 1){
					//保障金信誉度
					info.setShopMoneyCredibility(new BigDecimal("0.00"));
					//总信誉度
					info.setShopAllCredibility(BigDecimalUtil.add(info.getShopMoneyCredibility(), info.getShopCredibility()));
				}else if(shop.getShopBondStatus() == 2){
					List<Security> sList = securityMapper.findAll(new SecuritySearchForm());
					if(!sList.isEmpty()){
						//保障金信誉度
						info.setShopMoneyCredibility(new BigDecimal(sList.get(0).getSecurityCredibility()));
						//总信誉度
						info.setShopAllCredibility(BigDecimalUtil.add(info.getShopMoneyCredibility(), info.getShopCredibility()));
					}
				}
			}
			//保障金支付状态
			info.setShopBondStatus(shop.getShopBondStatus());
			//总保障金金额
			info.setShopAllMoney(shop.getShopAllMoney());
			
			mapper.update(info);
		}
	}
		
	
}
